{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# pyflow_example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/pyflow_example.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/pyflow_example.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "MaxFlow and MinCostFlow examples.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Sequence\n",
    "from ortools.graph.python import max_flow\n",
    "from ortools.graph.python import min_cost_flow\n",
    "\n",
    "\n",
    "def max_flow_api():\n",
    "    \"\"\"MaxFlow simple interface example.\"\"\"\n",
    "    print(\"MaxFlow on a simple network.\")\n",
    "    tails = [0, 0, 0, 0, 1, 2, 3, 3, 4]\n",
    "    heads = [1, 2, 3, 4, 3, 4, 4, 5, 5]\n",
    "    capacities = [5, 8, 5, 3, 4, 5, 6, 6, 4]\n",
    "    expected_total_flow = 10\n",
    "    smf = max_flow.SimpleMaxFlow()\n",
    "    for i in range(0, len(tails)):\n",
    "        smf.add_arc_with_capacity(tails[i], heads[i], capacities[i])\n",
    "    if smf.solve(0, 5) == smf.OPTIMAL:\n",
    "        print(\"Total flow\", smf.optimal_flow(), \"/\", expected_total_flow)\n",
    "        for i in range(smf.num_arcs()):\n",
    "            print(\n",
    "                \"From source %d to target %d: %d / %d\"\n",
    "                % (smf.tail(i), smf.head(i), smf.flow(i), smf.capacity(i))\n",
    "            )\n",
    "        print(\"Source side min-cut:\", smf.get_source_side_min_cut())\n",
    "        print(\"Sink side min-cut:\", smf.get_sink_side_min_cut())\n",
    "    else:\n",
    "        print(\"There was an issue with the max flow input.\")\n",
    "\n",
    "\n",
    "def min_cost_flow_api():\n",
    "    \"\"\"MinCostFlow simple interface example.\n",
    "\n",
    "    Note that this example is actually a linear sum assignment example and will\n",
    "    be more efficiently solved with the pywrapgraph.LinearSumAssignment class.\n",
    "    \"\"\"\n",
    "    print(\"MinCostFlow on 4x4 matrix.\")\n",
    "    num_sources = 4\n",
    "    num_targets = 4\n",
    "    costs = [[90, 75, 75, 80], [35, 85, 55, 65], [125, 95, 90, 105], [45, 110, 95, 115]]\n",
    "    expected_cost = 275\n",
    "    smcf = min_cost_flow.SimpleMinCostFlow()\n",
    "    for source in range(0, num_sources):\n",
    "        for target in range(0, num_targets):\n",
    "            smcf.add_arc_with_capacity_and_unit_cost(\n",
    "                source, num_sources + target, 1, costs[source][target]\n",
    "            )\n",
    "    for node in range(0, num_sources):\n",
    "        smcf.set_node_supply(node, 1)\n",
    "        smcf.set_node_supply(num_sources + node, -1)\n",
    "    status = smcf.solve()\n",
    "    if status == smcf.OPTIMAL:\n",
    "        print(\"Total flow\", smcf.optimal_cost(), \"/\", expected_cost)\n",
    "        for i in range(0, smcf.num_arcs()):\n",
    "            if smcf.flow(i) > 0:\n",
    "                print(\n",
    "                    \"From source %d to target %d: cost %d\"\n",
    "                    % (smcf.tail(i), smcf.head(i) - num_sources, smcf.unit_cost(i))\n",
    "                )\n",
    "    else:\n",
    "        print(\"There was an issue with the min cost flow input.\")\n",
    "\n",
    "\n",
    "def main(argv: Sequence[str]) -> None:\n",
    "    if len(argv) > 1:\n",
    "        raise app.UsageError(\"Too many command-line arguments.\")\n",
    "    max_flow_api()\n",
    "    min_cost_flow_api()\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
